<?php
function shDocSearch($condition){
	$secret_u = $_SESSION['edoc_secret'];
    $ans = getPG("select * from view_edoc_search ".$condition." and (secret<=$secret_u or depart_id_recieve=".$_SESSION['edoc_depart_id'].") order by depart_id_recieve,recieve_date desc,recieve_no::text::integer desc;");
    $_SESSION['report'] = "select * from view_edoc_search ".$condition." and (secret<=$secret_u or depart_id_recieve=".$_SESSION['edoc_depart_id'].") order by depart_id_recieve,recieve_date,recieve_no::text::integer;";

	if(!isset($ans[0])){
		echo "<span class=\"err\">ไม่มีรายการ</span><br />";
		return;
	}else if($ans==FALSE){
		echo "<span class=\"err\">การค้นหาผิดพลาด</span><br />";
		return;
	}
    runList($ans);
}

function shDocSearchSent($condition){
	$secret_u = $_SESSION['edoc_secret'];
    $ans = getPG("select * from view_edoc_search_sent ".$condition." and (secret<=$secret_u or depart_id_sent=".$_SESSION['edoc_depart_id'].") order by depart_id_sent,edoc_typesent_id,sent_date desc,sent_time desc;");
    $_SESSION['report'] = "select * from view_edoc_search_sent ".$condition." and (secret<=$secret_u or depart_id_sent=".$_SESSION['edoc_depart_id'].") order by depart_id_sent,edoc_typesent_id,sent_date,sent_time;";

	if(!isset($ans[0])){
		echo "<span class=\"err\">ไม่มีรายการ</span><br />";
		return;
	}else if($ans==FALSE){
		echo "<span class=\"err\">การค้นหาผิดพลาด</span><br />";
		return;
	}
    runListSent($ans);
}


function shDocSearchKsean($condition,$pname,$ksean_op,$depart_id){
	$secret_u = $_SESSION['edoc_secret'];
	if($ksean_op==0) $con2 = " and edoc_id not in (select edoc_id from view_edoc_search_ksean where pname_id=$pname)";
	else $con2 = " and edoc_id in (select edoc_id from view_edoc_search_ksean where pname_id=$pname)";

    $ans = getPG("select * from view_edoc_search ".$condition.$con2." and (secret<=$secret_u or depart_id_recieve=".$_SESSION['edoc_depart_id'].") order by depart_id_recieve,recieve_date desc,recieve_no desc;");

	if(!isset($ans[0])){
		echo "<span class=\"err\">ไม่มีรายการ</span><br />";
		return;
	}else if($ans==FALSE){
		echo "<span class=\"err\">การค้นหาผิดพลาด</span><br />";
		return;
	}
    runList($ans);

}

function runList($ans){
	echo "<table cellpadding=\"3\" cellspacing=\"0\" border=\"1\">";

	$count = 0;
	$start = 0;
	$stop = 0;
	$depart = $ans[0]['depart_id_recieve'];
	foreach($ans as $dat){
		if($dat['depart_id_recieve']==$depart){
			$stop = $count;
			$count = $count+1;
		}else{
			$dat2 = array_slice($ans,$start,$stop-$start+1);
			$start = $count;
			$stop = $count;
			$count = $count+1;
            echo "\n<tr><td colspan=\"13\">";
			echo "<span class=\"topic\">".get_depart_name($depart)."</span>";
            echo "</td></tr>";
			$depart2 = $depart;
			$depart = $dat['depart_id_recieve'];
			createDocList($dat2,true,$depart2);
		}
			
	}
	$dat2 = array_slice($ans,$start,$stop-$start+1);
    echo "\n<tr><td colspan=\"13\">";
	echo "<span class=\"topic\">".get_depart_name($depart)."</span>";
    echo "</td></tr>";
	createDocList($dat2,true,$depart);
	echo "\n</table>";
}

function runListSent($ans){
	echo "<table cellpadding=\"3\" cellspacing=\"0\" border=\"1\">";

	$count = 0;
	$start = 0;
	$stop = 0;
	$depart = $ans[0]['depart_id_sent'];
	foreach($ans as $dat){
		if($dat['depart_id_sent']==$depart){
			$stop = $count;
			$count = $count+1;
		}else{
			$dat2 = array_slice($ans,$start,$stop-$start+1);
			$start = $count;
			$stop = $count;
			$count = $count+1;
            echo "\n<tr><td colspan=\"13\">";
			echo "<span class=\"topic\">".get_depart_name($depart)."</span>";
            echo "</td></tr>";
			$depart2 = $depart;
			$depart = $dat['depart_id_sent'];
			listDocDoctype($dat2,$depart2);
		}
			
	}
	$dat2 = array_slice($ans,$start,$stop-$start+1);
    echo "\n<tr><td colspan=\"13\">";
	echo "<span class=\"topic\">".get_depart_name($depart)."</span>";
    echo "</td></tr>";
	listDocDoctype($dat2,$depart);
	echo "\n</table>";
}

function listDocDoctype($ans,$depart){
	$count = 0;
	$start = 0;
	$stop = 0;
	$doctypesent = $ans[0]['edoc_typesent_id'];
	foreach($ans as $dat){
		if($dat['edoc_typesent_id']==$doctypesent){
			$stop = $count;
			$count = $count+1;
		}else{
			$dat2 = array_slice($ans,$start,$stop-$start+1);
			$start = $count;
			$stop = $count;
			$count = $count+1;
            echo "\n<tr><td colspan=\"13\">";
			echo "<span class=\"err\">".get_doctypesent_name($doctypesent)."</span>";
            echo "</td></tr>";
			$doctypesent = $dat['edoc_typesent_id'];
			createDocList2($dat2,$depart);
		}
			
	}
	$dat2 = array_slice($ans,$start,$stop-$start+1);
    echo "\n<tr><td colspan=\"13\">";
	echo "<span class=\"err\">".get_doctypesent_name($doctypesent)."</span>";
    echo "</td></tr>";
	createDocList2($dat2,$depart);
}

function addSecretCon($condition,$query){
	$con = "";	
	foreach($query['secret'] as $dat){
		if($dat<=$_SESSION['edoc_secret']){
			if($con=="") $con = "(secret=$dat)";
			else $con = $con." or (secret=$dat)";
		}else{
			if($con=="") $con = "((secret=$dat) and (depart_id_recieve=".$_SESSION['edoc_depart_id']."))";
			else $con = $con." or ((secret=$dat) and (depart_id_recieve=".$_SESSION['edoc_depart_id']."))";
		}
	}
	if($con=="") return $condition;
	return $condition." (".$con.")"; 
}

function addSecretConSent($condition,$query){
	$con = "";	
	foreach($query['secret'] as $dat){
		if($dat<=$_SESSION['edoc_secret']){
			if($con=="") $con = "(secret=$dat)";
			else $con = $con." or (secret=$dat)";
		}else{
			if($con=="") $con = "((secret=$dat) and (depart_id_sent=".$_SESSION['edoc_depart_id']."))";
			else $con = $con." or ((secret=$dat) and (depart_id_sent=".$_SESSION['edoc_depart_id']."))";
		}
	}
	if($con=="") return $condition;
	return $condition." (".$con.")"; 
}

function addRapidCon($condition,$query){
	$con = "";	
	foreach($query['rapid'] as $dat){
		if($con=="") $con = "(rapid=$dat)";
		else $con = $con." or (rapid=$dat)";
	}
	if($con=="") return $condition;
	return $condition." and (".$con.")"; 
}

function addDoctypeCon($condition,$query){
	if($query['doctype']==0){
		if($query['depart_id_recieve']==0) $con = "";
		else $con = " and (depart_id_recieve=".$query['depart_id_recieve'].")";
	}
	else $con = " and (edoc_type_id=".$query['doctype'].")";
	return $condition.$con;
}

function addDoctypeConSent($condition,$query){
	if($query['doctypesent']==0){
		if($query['depart_id_sent']==0) $con = "";
		else $con = " and (depart_id_sent=".$query['depart_id_sent'].")";
	}
	else $con = " and (edoc_typesent_id=".$query['doctypesent'].")";
	return $condition.$con;
}

function addRecNo($condition,$recieve_no){
	$txt1 = trim($recieve_no);
	if($txt1 == "") return $condition;
	$con = " and (recieve_no::text::integer = ".$recieve_no."::text::integer)";
	return $condition.$con;
}

function addTxtCon($condition,$txt,$column){
	$txt1 = trim($txt);
	if($txt1 == "") return $condition;
	$txt2 = explode(" ", $txt1);
	$con = "";
	foreach($txt2 as $dat){
		if($con=="") $con = "($column like '%".$dat."%')";
		else $con = $con." and ($column like '%".$dat."%')";
	}
	if($con=="") return $condition;
	return $condition." and (".$con.")"; 
}

function addDateCon($condition,$d1,$d2,$column){
	$dd1 = trim($d1);
	$dd2 = trim($d2);
	$con1 = "";
	$con2 = "";	
	
	if(($dd1!="") && ($dd2=="")) $con1 = " and ($column = '".dateThEnco($dd1)."')";
	
	if(($dd1!="") && ($dd2!="")) $con1 = " and ($column >= '".dateThEnco($dd1)."')";

	if($dd2!="") $con2 = " and ($column <= '".dateThEnco($dd2)."')";
	
	return $condition.$con1.$con2;
}

function addDepartSend($condition,$depart,$depart_send){
	if($depart==0) return $condition;
	else if($depart==1){
		$d1 = trim($depart_send);
		$d2 = explode(" ",$d1);
		$con = "";
		foreach($d2 as $dat){
			if($con=="") $con = "(depart_send_name_out like '%".$dat."%')";
			else $con = $con." and (depart_send_name_out like '%".$dat."%')";
		}
		$con = "(".$con.")";
		$con = " and (".$con." and (depart_send_id = $depart))";
		return $condition.$con;
	}
	else{
		return $condition." and (depart_send_id = $depart)";
	}
}

function get_position_searchx(){
?>
<table cellpadding="5" cellspacing="0" border="0">
<tr><td class="topic">ผู้ดำรงตำแหน่ง</td><td>:</td><td>
<span id="pname_dat">
<?php
    $pos = list_position1(0);
    echo "<select name=\"pname\" size=\"1\">\n";	
	if($pos==0) echo "<option value=\"0\">ไม่มีผู้ดำรงตำแหน่ง</option>";
	else{	
	    $ans = getPG("select * from view_pname where position_id=".$pos.";");

	    foreach($ans as $dat){
		    echo "<option value=\"".$dat['pname_id']."\">".$dat['pname_name']."</option>\n";
	    }
	
    }
    echo "</select>\n";

?>
</span>
</td></tr>
<tr><td class="topic">ตำแหน่ง</td><td>:</td><td>
<select name="position" size="1" id="xposition" onchange="pnamech()">
<?php list_position_option(0); ?>
</select>
</td></tr>

<tr><td colspan="3">
<optgroup label="ksean_op">
<input type="radio" name="ksean_op" value="0" checked >ยังไม่ได้เกษียณ &nbsp;&nbsp;&nbsp;
<input type="radio" name="ksean_op" value="1" >เกษียณแล้ว &nbsp;&nbsp;&nbsp;
</optgroup>
</td></tr>
</table>

<?php
}
?>
